#include "aerial\Object\Text.h"

#include "aerial\Scene\SceneManager.h"
#include "aerial\Scene\Event.h"
#include <stdio.h>

using namespace air;

Text::Text(hgeResourceManager* hgeResMan, float x, float y, float w, float h, Event* _evnt)
	:IGuiObject(hgeResMan), evnt(_evnt), mouseOver(false)
{
	font = hgeResMan->GetFont("fnt.original");
	ox = x;
	oy = y;
	backgroundColor = new hgeSprite(0, 0, 0, w, h);
	backgroundColor->SetColor(0xff479DF3);
	align = HGETEXT_LEFT;
	bStatic=true;
	bVisible=true;
	bEnabled=true;
	rect.Set(x, y, x+w, y+h);

	text[0] = 0;
}

Text::Text(hgeResourceManager* hgeResMan, float w, float h, Event* _evnt)
	:IGuiObject(hgeResMan), evnt(_evnt), mouseOver(false)
{
	font = hgeResMan->GetFont("fnt.original");
	backgroundColor = new hgeSprite(0, 0, 0, w, h);
	backgroundColor->SetColor(0xff479DF3);
	align = HGETEXT_LEFT;
	bStatic=true;
	bVisible=true;
	bEnabled=true;
	rect.Set(0, 0, w, h);

	text[0]=0;
}

Text::~Text()
{
	delete backgroundColor;
}

void Text::SetMode(int _align)
{
	align=_align;
}

void Text::SetText(const char *_text)
{
	strcpy(text, _text);
}

void Text::printf(const char *format, ...)
{
	vsprintf(text, format, (char *)&format+sizeof(format));
}

void Text::MouseOver(bool bOver)
{
	mouseOver = bOver;
}

bool Text::MouseLButton(bool bDown)
{
	if (!bDown && evnt) 
	{
		return true;
	}
	else return false;
}

void Text::Render()
{
	isPositionUpdate = true;
	calcAbsolutePosition();
	
	if( align == HGETEXT_RIGHT )  tx = rect.x2;
	else if( align == HGETEXT_CENTER ) tx = ( rect.x1 + rect.x2 ) / 2.0f;
	else tx = rect.x1;
	ty = rect.y1 + ( rect.y2 - rect.y1 - font->GetHeight() ) / 2.0f;

	font->SetColor(color);
	
	if (evnt && mouseOver)
	{
		backgroundColor->Render(rect.x1, rect.y1);
	}
	font->Render(tx,ty,align,text);
}

bool Text::addToGui(SceneManager* sceneMan, IGuiObject* _anchor)
{
	sceneMan->addGuiObject(this);
	sceneMan->bindGuiObjectEvent(id, evnt);
	anchor = _anchor;
	return true;
}
